<?
header("Content-Type: application/xml; charset=ISO-8859-1");
include('../controller/handler.php');
include('../controller/twitwords.php');
include('../scaffold/user.php');
include('../scaffold/tag.php');

$hd = new Handler();
$tw = new Twitwords();
$hd->openDB();
$base_url = "http://".$_SERVER['SERVER_NAME'].$hd->getBaseURL();
if($_GET['id']!="twitwords") {
	$user = $tw->loadUserByName($_GET['id']);
	$tags = $tw->loadTags($user);
} else {
	$tags = $tw->loadAllTags(0, $hd->getTagsInRSS());
}
$hd->closeDB();
?>
<rss version="2.0">
	<channel>
<?if($_GET['id']!="twitwords") {?>
		<title><?echo $user->getUsername();?>'s tags</title>
		<description>Feed of all <?echo $user->getUsername();?>'s tags</description>
		<image>
			<url><?echo $user->getImage();?></url>
			<title><?echo $user->getUsername();?>'s avatar</title>
			<link>http://twitter.com/<?echo $user->getUsername();?></link>
		</image>
		<link><?echo $base_url;?>user/<?echo $user->getUsername();?></link>
<?} else {?>
	<title>Twitwords</title>
	<description>Feed of last <?echo $hd->getTagsInRSS();?> tags</description>
	<link><?echo $base_url;?></link>
<?}?>
		<lastBuildDate><?echo date("r", time());?></lastBuildDate>
		<language>en-us</language>
		<generator>Twitwords</generator>
<?foreach($tags as $tag) {?>
		<item>
			<title><?echo $tag->getTag();?></title>
<?if($_GET['id']!="twitwords") {?>
			<link><?echo $base_url;?>search?tag=<?echo urlencode($tag->getTag());?></link>
			<description><![CDATA[ Tag inserted @ <?echo date($hd->getDateFormat(), strtotime($tag->getDate()));?> ]]></description>
			<author><?echo $user->getUsername();?></author>
<?} else {?>
			<link><?echo $base_url;?>user/<?echo $tag->getUser()->getUsername();?></link>
			<description><![CDATA[ Tag inserted @ <?echo date($hd->getDateFormat(), strtotime($tag->getDate()));?>
				by <?echo $tag->getUser()->getUsername();?> ]]></description>
			<author><?echo $tag->getUser()->getUsername();?></author>
<?}?>
			<category><?echo $tag->getPriority();?></category>
			<pubDate><?echo date("r", strtotime($tag->getDate()))?></pubDate>
		</item>
<?}?>
	</channel>
</rss>